const mongoose = require('mongoose');
const Resource = require('../models/Resource');
require('dotenv').config();

const resources = [
  // 回收点
  {
    title: "绿源回收站",
    type: "回收点",
    description: "专业的可回收物回收站点，提供废纸、塑料、金属、电子产品等回收服务。配备专业分类设备，提供上门回收服务。",
    address: "北京市海淀区中关村大街28号",
    location: [116.3276, 39.9871],
    status: "active",
    tags: ["废纸回收", "塑料回收", "金属回收", "电子回收"],
    openHours: "周一至周日 8:00-18:00",
    contact: "010-88886666"
  },
  {
    title: "智慧社区回收点",
    type: "回收点",
    description: "24小时智能回收站，配备智能分类设备，可自动识别和分类回收物，提供积分奖励。",
    address: "北京市朝阳区建国路88号",
    location: [116.4655, 39.9097],
    status: "active",
    tags: ["智能回收", "24小时服务", "积分奖励"],
    openHours: "24小时营业",
    contact: "400-888-8888"
  },
  {
    title: "绿色家园回收中心",
    type: "回收点",
    description: "综合性回收中心，专注于大件物品和装修废料的回收处理，提供免费上门估价服务。",
    address: "北京市海淀区西二旗大街120号",
    location: [116.3095, 40.0516],
    status: "active",
    tags: ["大件回收", "装修废料", "免费上门"],
    openHours: "周一至周六 9:00-17:00",
    contact: "010-66668888"
  },
  {
    title: "环保再生站",
    type: "回收点",
    description: "专注于废旧电器和电子产品的回收和再生利用，提供专业的数据销毁服务。",
    address: "北京市朝阳区望京科技园18号",
    location: [116.4847, 40.0087],
    status: "active",
    tags: ["电子回收", "数据销毁", "再生利用"],
    openHours: "周一至周五 9:00-18:00",
    contact: "010-99995555"
  },
  {
    title: "社区环保驿站",
    type: "回收点",
    description: "便民回收点，提供日常可回收物和有害垃圾回收服务，设有环保知识宣传角。",
    address: "北京市海淀区清华园1号",
    location: [116.3331, 40.0093],
    status: "active",
    tags: ["社区回收", "有害垃圾", "环保宣传"],
    openHours: "周一至周日 9:00-20:00",
    contact: "010-77779999"
  },

  // 环保活动
  {
    title: "跳蚤市场嘉年华",
    type: "活动",
    description: "大型二手物品交换和售卖活动，倡导可持续消费理念。现场还有环保手工制作工作坊。",
    address: "北京市朝阳区朝阳公园广场",
    location: [116.4836, 39.9921],
    status: "active",
    time: "2024-04-20 10:00-16:00",
    tags: ["二手交易", "手工制作", "可持续消费"],
    contact: "13812345678",
    startDate: new Date(),
    endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
    checkInRequired: 3,
    participants: []
  },
  {
    title: "垃圾分类达人赛",
    type: "活动",
    description: "趣味垃圾分类比赛，设有理论和实践两个环节，优胜者可获得精美环保礼品。",
    address: "北京市海淀区市民文化中心",
    location: [116.3185, 39.9981],
    status: "active",
    time: "2024-04-15 14:00-16:00",
    tags: ["垃圾分类", "趣味比赛", "环保教育"],
    contact: "13987654321",
    startDate: new Date(),
    endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
    checkInRequired: 3,
    participants: []
  },
  {
    title: "地球日环保行动",
    type: "活动",
    description: "世界地球日主题活动，包括公园清洁、植树、环保讲座等多个环节。",
    address: "北京市海淀区圆明园遗址公园",
    location: [116.3031, 40.0079],
    status: "active",
    time: "2024-04-22 09:00-17:00",
    tags: ["地球日", "环境保护", "志愿服务"],
    contact: "13598765432",
    startDate: new Date(),
    endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
    checkInRequired: 3,
    participants: []
  },
  {
    title: "绿色生活工作坊",
    type: "活动",
    description: "零废弃生活方式分享会，包括环保酵素制作、旧物改造、无塑包装等实践课程。",
    address: "北京市朝阳区三里屯SOHO",
    location: [116.4545, 39.9339],
    status: "active",
    time: "2024-04-28 13:30-16:30",
    tags: ["零废弃", "生活方式", "手作课程"],
    contact: "13866668888",
    startDate: new Date(),
    endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
    checkInRequired: 3,
    participants: []
  },
  {
    title: "社区环保市集",
    type: "活动",
    description: "每月一次的环保主题市集，有机农产品直销、环保产品展示、废物利用艺术展等。",
    address: "北京市海淀区五道口购物中心",
    location: [116.3374, 39.9931],
    status: "active",
    time: "2024-05-01 10:00-18:00",
    tags: ["环保市集", "有机食品", "艺术展览"],
    contact: "13900001111",
    startDate: new Date(),
    endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
    checkInRequired: 3,
    participants: []
  },
  {
    title: "社区环保活动",
    type: "活动",
    description: "社区垃圾分类宣传活动",
    address: "北京市海淀区中关村",
    location: [116.3276, 39.9871],
    status: "active",
    startDate: new Date(),
    endDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
    checkInRequired: 3,
    participants: []
  }
];

async function initResources() {
  try {
    console.log('开始初始化资源...');
    console.log('连接数据库:', process.env.MONGODB_URI);
    
    await mongoose.connect(process.env.MONGODB_URI);
    console.log('数据库连接成功');

    // 清除现有资源
    const deleteResult = await Resource.deleteMany({});
    console.log('清除现有资源:', deleteResult);

    // 插入新资源
    const result = await Resource.insertMany(resources);
    console.log(`成功创建 ${result.length} 个资源`);
    
    // 验证插入的数据
    const count = await Resource.countDocuments();
    console.log('数据库中的资源总数:', count);
    
    const activeResources = await Resource.find({ status: 'active' });
    console.log('活跃资源数量:', activeResources.length);
    console.log('活跃资源列表:', activeResources.map(r => ({
      title: r.title,
      type: r.type,
      status: r.status
    })));

    console.log('资源初始化完成');
  } catch (error) {
    console.error('初始化资源失败:', error);
  } finally {
    await mongoose.connection.close();
  }
}

// 立即执行初始化
initResources(); 